package com.bj58.ecdata.redis;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;

import com.bj58.ecdata.redis.JedisX;
import com.bj58.ecdata.redis.JedisXFactory;
import com.bj58.ecdata.redis.RedisData.RedisKeyMemStr;
import com.bj58.ecdata.redis.RedisSortData.RedisKeySortMemStr;

import redis.clients.jedis.Jedis;

public class testRedis {

	public static void main(String[] sadf) throws Exception{
		//testPipZscoreString_insert();
		//testPipZscoreString();
		testPipZaddStr();
		testPipZscoreString();
		testPipZrmStr();
		testPipZscoreString();
	}
	
	private static int timeout = 300;
	public static void testPipZaddStr(){
		long start=System.currentTimeMillis();
		JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		List<RedisKeySortMemStr>  requests=new ArrayList<RedisKeySortMemStr>();
		for(int i=0;i<100000;i++){
			for(int j=0;j<2;j++){
				requests.add(new RedisKeySortMemStr("zaddkey_forid_"+i,"mem_"+j,i*(j+1)));
			}
		}
		cache.pipZaddStr(requests);
		System.out.println("=============="+(System.currentTimeMillis()-start));
	}
	
	public static void testPipZrmStr(){
		long start=System.currentTimeMillis();
		JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		List<RedisKeyMemStr>  requests=new ArrayList<RedisKeyMemStr>();
		for(int i=0;i<100000;i++){
			for(int j=0;j<2;j++){
				requests.add(new RedisKeyMemStr("zaddkey_forid_"+i,"mem_"+j));
			}
		}
		cache.pipZremStr(requests);
		System.out.println("=============="+(System.currentTimeMillis()-start));
	}
	
	
	public static void testPipZscoreString(){
		long start=System.currentTimeMillis();
		JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		List<RedisKeyMemStr>  requests=new ArrayList<RedisKeyMemStr>();
		for(int i=0;i<100000;i++){
			for(int j=0;j<2;j++){
				requests.add(new RedisKeyMemStr("zaddkey_forid_"+i,"mem_"+j));
			}
		}
		Map<RedisKeyMemStr, Double> result=cache.pipZscoreString(requests);
		
		System.out.println(result.keySet().size()+"=============="+(System.currentTimeMillis()-start));
		int i=0;
		for(RedisKeyMemStr memStr:result.keySet()){
			i++;
			if(i%100==0)
			System.out.println(memStr+"::"+result.get(memStr));
		}
	}
	
	
	public static void testPipZscoreString_insert(){
		long start=System.currentTimeMillis();
		final JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=0;i<10000;i++){
					for(int j=0;j<2;j++){
						cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
					}
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=10000;i<20000;i++){
					for(int j=0;j<2;j++){
						cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
					}
				}
			}
		}).start();
		
		
new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=20000;i<30000;i++){
					for(int j=0;j<2;j++){
						cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
					}
				}
			}
		}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=30000;i<40000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=40000;i<50000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=50000;i<60000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=60000;i<70000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=70000;i<80000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=80000;i<90000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
		System.out.println("===");
	}
}).start();
new Thread(new Runnable() {
	
	@Override
	public void run() {
		for(int i=90000;i<100000;i++){
			for(int j=0;j<2;j++){
				cache.zaddString("zsortkey_forid_"+i, i*(j+1), "mem_"+j);
			}
		}
		System.out.println("===");
	}
}).start();
	}
	
	
	
	
	public static void testPipline_pip(){
		long start=System.currentTimeMillis();
		JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		List<String> keys=new ArrayList<String>();
		for(int i=0;i<200000;i++){
			keys.add("test_pipline_for_timeline_id_"+i);
		}
		Map<String, Boolean> result=cache.pipExists(keys);
		System.out.println("=============="+(System.currentTimeMillis()-start));
	
		
		
	}
	
	
	public static void testPipline_pip_thread(int threadNum){
		final long start=System.currentTimeMillis();
		final JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		for(int j=0;j<threadNum;j++){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				List<String> keys=new ArrayList<String>();
				for(int i=0;i<200000;i++){
					keys.add("test_pipline_for_timeline_id_"+i);
				}
				Map<String, Boolean> result=cache.pipExists(keys);
				System.out.println("=============="+(System.currentTimeMillis()-start));
				
			}
		}).start();
		
		}
		
	}
	

	
	public static void testPipline_thread(int threadNum){
		final long start=System.currentTimeMillis();
		final JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		final LinkedBlockingQueue<String> keys=new LinkedBlockingQueue<String>();
		final AtomicLong al=new AtomicLong();
		for(int i=0;i<200010;i++){
			keys.add("test_pipline_for_timeline_id_"+i);
		}
		for(int i=0;i<threadNum;i++)
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try{
					for(;;){
						long result=al.addAndGet(1);
						if(result>200000){
							System.out.println("=============="+(System.currentTimeMillis()-start));
							break;
						}
						String key=keys.take();
						cache.exists(key);
					}
				}catch(Exception e){
					
				}
			}
		}).start();
		
		
	
	
	}
	
	
	public static void testPipline_insert(){
		long start=System.currentTimeMillis();
		JedisX cache=JedisXFactory.getJedisX("10.11.152.109:6379",timeout);
		List<String> keys=new ArrayList<String>();
		for(int i=0;i<200000;i++){
			String key="test_pipline_for_timeline_id_"+i;
			if(i%4==0){
				cache.setString(key, 60*60*24, "1");
			}
		}
		
	}
	
	public static void testDeleteColumnCache() throws InterruptedException{
		JedisX cache=JedisXFactory.getJedisX("10.10.68.151:6401,10.10.68.152:6401,10.10.68.153:6401",timeout);
		for(int i=0;i<4000;i++){
			long  status=	cache.delete("smc_video_com.kingsoft.smc.domain.MessagePoolColumnTimeline__list___columnId_"+i+"_status_0");
			if(status==1){
			System.out.println("delete:"+i);
				Thread.sleep(2000);
			}
		}
		
		
	}
	
	public static void testDeleteCache(){
		JedisX cache=JedisXFactory.getJedisX("10.10.68.151:6401,10.10.68.152:6401,10.10.68.153:6401",timeout);
		
		
		System.out.println(cache.exists("smc_timeline_object_com.kingsoft.smc.timeline.domain.TimelineInfo_id_1"));
		cache.delete("smc_timeline_object_com.kingsoft.smc.timeline.domain.TimelineInfo_id_1");
		cache.delete("smc_timeline_object_com.kingsoft.smc.timeline.domain.TimelineInfo_id_2");
	}
	
	
	public static void testgetQps() throws Exception{
		Jedis jedis=new Jedis("10.10.68.151", 6379);
		long lasTimes=0;

		for(;;){
		String info=jedis.info();
		
		info=info.substring(info.indexOf("total_commands_processed:")+"total_commands_processed:".length(),info.indexOf("expired_keys"));
		long times=Long.parseLong(info.trim().replace("\r\n", ""));
		System.out.println(new Date()+"："+(times-lasTimes));
		lasTimes=times;
		Thread.sleep(1000);
		}
	}
	
	
}
